﻿using System;
using System.IO;
using System.Text;

namespace ExRev.Engine.Utils
{
    /// <summary>
    /// Defines the LogLevel
    /// </summary>
    public enum LogLevel
    {
        Debug,
        Info,
        Warn,
        Error,
        Fatal,
        Verbose
    }

    public abstract class Logger
    {
        /// <summary>
        /// Defines the Type of the Logger
        /// </summary>
        public enum Type
        {
            Html,
            Text
        }

        /// <summary>
        /// Name of the logger
        /// </summary>
        public string Name
        {
            get { return name; }
        }
        private string name;

        /// <summary>
        /// Path of the logger
        /// </summary>
        internal string Path
        {
            get { return path; }
            set { path = value; }
        }
        private string path;

        /// <summary>
        /// Full name and path of the logger
        /// </summary>
        protected string FullPath
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                return sb.Append(path).Append("\\").Append(name).Append(extention).ToString();
            }
        }

        /// <summary>
        /// Extention of the logger
        /// </summary>
        protected string Extention
        {
            get { return extention; }
            set { extention = value; }
        }
        private string extention;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="name">Name of the Logger</param>
        /// <param name="path">Specific path for the Logger</param>
        protected Logger(string name, string path)
        {
            if (string.IsNullOrEmpty(name)) {
                throw new LogNameNotSetException("The name of the logger is null or empty");
            }
            this.name = name;
            this.path = path;
        }

        /// <summary>
        /// Tries to log the requested message
        /// </summary>
        /// <param name="level">Logging level</param>
        /// <param name="message">Message to be written</param>
        /// <param name="exception">LogPathNotSetException</param>
        /// <returns>Logging was either successful or not</returns>
        public abstract bool Log(LogLevel level, string message);

        /// <summary>
        /// Writes the prepared message to the specified path
        /// </summary>
        /// <param name="message">Message</param>
        /// <returns>Message has been written successfully or not</returns>
        protected virtual bool Write(string message)
        {
            using (StreamWriter sw = new StreamWriter(FullPath, true)) {
                try {
                    sw.Write(message);
                } catch {
                    return false;
                }
            }
            return true;
        }
    }
}
